package com.lg.spring;

import com.lg.spring.echo.DefaultEchoService;
import com.lg.spring.echo.EchoService;
import com.lg.spring.echo.EchoWebSocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.PostConstruct;

/**
 * JDWP： Java Debug Wire Protocol,Java调试协议
 */
@SpringBootApplication
@ServletComponentScan // 开启Servlet扫描
@EnableWebSocket // 开启webSocket
//@EnableKafka
public class App implements WebSocketConfigurer {

	private final static Logger LOGGER = LoggerFactory.getLogger(App.class);

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);

		//SpringApplication springApplication = new SpringApplication(App.class);
		//springApplication.setBannerMode(Banner.Mode.OFF);
		//
		//springApplication.run(args);

		// 直接右键运行： 类加载器 是 AppClassLoader
	}

	/**
	 * 注册 webSocket处理器
	 *
	 * @param registry
	 */
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(echoWebSocketHandler(), "/echo").withSockJS();
	}

	@Bean
	public EchoService echoService() {
		return new DefaultEchoService("You said \"%s\"!");
	}

	@Bean
	public WebSocketHandler echoWebSocketHandler() {
		return new EchoWebSocketHandler(echoService());
	}

	/**
	 * 日志相关逻辑
	 */
	//@PostConstruct
	//public void myLog() {
	//	LOGGER.trace("Trace Message"); // 最低级别
	//	LOGGER.debug("Debug Message");
	//	LOGGER.info("Info Message");
	//	LOGGER.warn("Warn Message");
	//	LOGGER.error("Error Message");
	//}
}
